package Controller;

import DataAccess.DataAccessFacade;
import DomainModel.Grade;
import DomainModel.Role;
import DomainModel.Section;
import DomainModel.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.ServerException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ReynierO
 * @date 11/10/2013
 */
public class ViewMyGradesController extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        
        if (user == null) {
            request.getRequestDispatcher("/login").forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        
        if (user.getRole() != Role.STUD) {
            throw new ServletException("Forbidden");
        }
        
        try {
            int sectionId = Integer.valueOf(request.getParameter("sid"));
            DataAccessFacade dataAccessFacade = DataAccessFacade.getInstance();
            
            Section section = dataAccessFacade.getSectionByIdAndUser(sectionId, user.getUserId());
            List<Grade> grades = dataAccessFacade.getStudentGrades(user.getUserId(), Integer.valueOf(sectionId));
            
            if (user.enrolledInSection(sectionId)) {
                request.setAttribute("section", section);
                request.setAttribute("grades", grades);
                request.getRequestDispatcher("/WEB-INF/grs/viewMyGrades.jsp").forward(request, response);
            } else {
                throw new ServletException("Forbidden");
//                request.getRequestDispatcher("/WEB-INF/grs/viewCourseSections.jsp").forward(request, response);                
            }
        } catch (Exception e) {
           throw new ServletException("Forbidden");
        }        
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        String actionBackToCourses = request.getParameter("backToCourses");        
        
        if (actionBackToCourses != null) {
            response.sendRedirect("viewCourseSections");
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
